﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using hachiModel;

namespace Hachi.Controllers
{
    public class NewsController : BaseController
    {
        public NewsController()
        {
            
        }
        public NW_News NewsGetByID(int pNewsID)
        {
            return en.NW_News.Where(d => d.NewsID == pNewsID).FirstOrDefault();
        }
        public NW_News NewsGetByID(string pAlias)
        {
            NW_NewsDetail nd = en.NW_NewsDetail.Where(d => d.Alias == pAlias).FirstOrDefault();
            return nd.NW_News;
        }
        public NW_NewsDetail NewsDetailGet(NW_News pNews, string Lang)
        {
            NW_NewsDetail nd = pNews.NW_NewsDetail.Where(d => d.Language == Lang).FirstOrDefault();
            if (nd==null)
            {
                nd = new NW_NewsDetail();
            }
            return nd;
        }

        public List<NW_News> NewsGetTopHit(int top, string Lang)
        {
            return en.NW_News.Where(d=>
                d.IsApproved==true
                &&d.IsDeleted==false
                &&(d.NW_NewsDetail.Where(c=>c.Language==Lang).Count())>0)
                .OrderByDescending(o => o.Hit).ThenByDescending(o=>o.CreatedDate).Take(top).ToList();
        }
        public List<NW_News> NewsGetTopNewPost(int top, string Lang)
        {
            return en.NW_News.Where(d => d.IsApproved == true &&
                d.IsDeleted == false &&
                (d.NW_NewsDetail.Where(c => c.Language == Lang).Count()) > 0)
                .OrderByDescending(o => o.CreatedBy).Take(top).ToList();
        }

        public List<NW_News> NewsGetTopNewPost(int top, int NewsCategoryID, string Lang)
        {
            return en.NW_News.Where(d => d.IsApproved == true && 
                d.IsDeleted == false && 
                d.CategoryID == NewsCategoryID && 
                (d.NW_NewsDetail.Where(c => c.Language == Lang).Count()) > 0)
                .OrderByDescending(o => o.CreatedBy).Take(top).ToList();
        }
        public int NewsCount(int NewsCategoryID, string Lang)
        {
            return en.NW_News.Where(d => d.IsApproved == true && 
                d.IsDeleted == false && 
                d.CategoryID == NewsCategoryID && 
                (d.NW_NewsDetail.Where(c => c.Language == Lang).Count()) > 0).Count();
        }
        public List<NW_News> NewsGetByCategoryID(int NewsCategoryID, string Lang, int Take, int Skip)
        {
            return en.NW_News.Where(d => d.IsApproved == true &&
                d.IsDeleted == false &&
                d.CategoryID == NewsCategoryID &&
                (d.NW_NewsDetail.Where(c => c.Language == Lang).Count()) > 0)
                .OrderBy(o => o.SortOrder).ThenByDescending(o => o.CreatedDate)
                .Skip(Skip).Take(Take).ToList();
        }
    }
}